﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MM.Common.Setup;
using MM.Commmon.UI.Controls;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MM.Common.DBBase;
using MM.Common.Base;
using System.Data;
using Microsoft.Reporting.WinForms;
using System.Drawing;
using DevExpress.XtraEditors.Controls;
using DevExpress.Utils;

namespace MM.Common.Support
{
    public partial class FrmErrorView : FrmViewRDLC
    {
        private DateTime dt1;
        private DateTime dt2;
        public FrmErrorView()
        {
            InitializeComponent();
            this.dateTimePicker1.DateTime = DateTime.Today;
            this.dateTimePicker2.DateTime = DateTime.Today;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("是否真的清除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("dt1", this.dt1), new InputDataParam("dt2", this.dt2) });
                if (SystemInfo.DbData.ExecuteSql("DELETE FROM MXHLY_SYS_ERROR WHERE ERROR_TIME >= :dt1 AND ERROR_TIME <= :dt2", parms) > 0)
                {
                    XtraMessageBox.Show("清除记录成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    ((DataTable)base.rptViewer1.LocalReport.DataSources[0].Value).Rows.Clear();
                    base.rptViewer1.RefreshReport();
                    this.btnDelete.Enabled = false;
                }
                else
                {
                    XtraMessageBox.Show("清除记录不成功！\r\n" + SystemInfo.DbData.ErrorMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }

        private void btnFresh_Click(object sender, EventArgs e)
        {
            this.dt1 = this.dateTimePicker1.DateTime.Date;
            this.dt2 = this.dateTimePicker2.DateTime.Date.AddDays(1.0);
            this.dt2 = this.dt2.AddSeconds(-1.0);
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("input01", this.dt1), new InputDataParam("input02", this.dt2) });
            try
            {
                if (base.InitReport(SystemInfo.RunPath + @"REPORT\SysError.rdlc"))
                {
                    if (base.AddDataSource(parm))
                    {
                        ReportParameter[] rparm = new ReportParameter[] { new ReportParameter("input01", this.dt1.ToString("yyyy-MM-dd")), new ReportParameter("input02", this.dt2.ToString("yyyy-MM-dd")) };
                        base.DisplayReport(rparm);
                        this.btnDelete.Enabled = SystemInfo.OperIsAdmin && (((DataTable)base.rptViewer1.LocalReport.DataSources[0].Value).Rows.Count > 0);
                    }
                }
                else
                {
                    XtraMessageBox.Show("报表格式文件(SysError.rdlc)不存在或错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
            catch (Exception exception)
            {
                XtraMessageBox.Show(exception.Message);
            }
        }

        private void FrmErrorView_Shown(object sender, EventArgs e)
        {
            base.ZoomWin();
            this.btnDelete.Visible = SystemInfo.OperIsAdmin;
        }
    }
}
